package com.recharge.web.controller;


import com.recharge.common.domain.PageDO;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.result.web.ObjectResponse;
import com.recharge.common.util.ExcelUtil;
import com.recharge.common.util.TemplateUtils;
import com.recharge.domain.query.RefundQueryDO;
import com.recharge.domain.vo.RefundDO;
import com.recharge.service.RefundService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 退款单 前端控制器
 * </p>
 *
 * @author reagan
 * @since 2020-08-19
 */
@RestController
@RequestMapping("/refund")
public class RefundController extends BaseController {

    @Value("${file.refund.template}")
    private String fileTemplatePath;

    @Autowired
    RefundService refundService;

    @RequiresPermissions("refund:page")
    @RequestMapping(value = "/page.html", method = RequestMethod.POST)
    @ApiOperation(value = "查询退款记录")
    public @ResponseBody
    ObjectResponse page(@RequestBody RefundQueryDO query) {
        ObjectResponse response = new ObjectResponse();

        ResultDO result = refundService.findInfo(query);
        if (result.isSuccess()) {
            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
        } else {
            response.setStatus(result.getErrorCode().getCode());
            response.setMsg(result.getErrorCode().getMsg());
        }
        return response;
    }

    @RequiresPermissions("refund:excel-download")
    @RequestMapping(value = "/excelDownload.html", method = RequestMethod.POST)
    @ApiOperation(value = "excel文件导出退款记录", notes = "")
    public void excelDownload(@RequestBody RefundQueryDO query, HttpServletResponse response) {
        ResultDO result = refundService.findInfo(query);
        PageDO<RefundDO> lists = (PageDO<RefundDO>) result.getModel(ResultDO.FIRST_MODEL_KEY);
        List<RefundDO> list = lists.getData();
        List<Object> downloadList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> data = new HashMap<>();
            data.put("a1", item.getRefundNo());
            data.put("a2", item.getMerchantName());
            data.put("a3", item.getMerchantNo());
            data.put("a4", item.getAmount() / 1000);
            data.put("a5", item.getType() == 0 ? "自动退款" : "人工退款");
            data.put("a6", (!TemplateUtils.isEmpty(item.getBeforeBalance())) ? item.getBeforeBalance() / 1000 : "");
            data.put("a7", (!TemplateUtils.isEmpty(item.getAfterBalance())) ? item.getAfterBalance() / 1000 : "");
            data.put("a8", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.getRefundDate()));
            data.put("a9", item.getStatus() == 1 ? "已退款" : "未退款");
            data.put("a10", item.getRemark());
            downloadList.add(data);
        });
        ExcelUtil.exportExcel(response, downloadList, fileTemplatePath, "退款记录列表.xls");

    }

//    @Deprecated
////    @RequiresPermissions("refund:submit")
//    @RequestMapping(value="/submit.html", method = RequestMethod.POST)
//    @ApiOperation(value="手动退款", notes="")
//    public @ResponseBody
//    ObjectResponse submit(@RequestBody RefundDO domain) {
//
//        ObjectResponse response = new ObjectResponse<>();
//        domain.setType(Constant.REFUND_TYPE_MANUAL);
//        ResultDO result = refundService.submit(domain);
//
////        if(!result.isSuccess()) {
////            response.setStatus(result.getErrorCode().getCode());
////            response.setMsg(result.getErrorCode().getMsg());
////        }
//
//        if (result.isSuccess()){
//            response.setData(result.getModel(ResultDO.FIRST_MODEL_KEY));
//        }else {
//            response.setStatus(result.getErrorCode().getCode());
//            response.setMsg(result.getErrorCode().getMsg());
//        }
//        return response;
//    }

}
